<template>
  <el-card class="login-form">
    <el-form  :model="form" label-width="auto" style="max-width: 600px">
      <h1 class="title">登录</h1>
      <el-form-item label="账号：">
        <el-input v-model="form.name" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="密码：">
        <el-input v-model="form.password"   type="password"
      placeholder="请输入密码" />
      </el-form-item>
      <el-button type="primary" @click="onSubmit">登录</el-button>
      <el-button  @click="reset">重置</el-button>
    </el-form>
</el-card>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router';
import {ElMessage} from 'element-plus';
// do not use same name with ref
const form = reactive({
  name: 'admin',
  password: '123456',
})
const userinfo = reactive([{
  id: '',
  user: 'admin',
  password: '123456',
  role: 'admin'
},{
  id: '1',
  user: 'admin',
  password: '123456',
  role: 'admin'
},{
  id: '2',
  user: 'userB',
  password: '123456',
  role: 'user'
}])
const onSubmit = () => {
  let res = userinfo.find((item) => {
    return item.user === form.name && item.password === form.password
  })
  if(res?.role === 'admin'){
    goAmdin()
  }else if(res?.role==='user'){
    goUser()
  }else{
    ElMessage.error('账号或密码错误');
  }

}
const router = useRouter()
function goAmdin() {
  router.push({
    path: '/home'
  })
}

function goUser(){
  router.push({
    path: '/user'
  })
}

function reset(){
  form.name = '';
  form.password = ''
}
</script>

<style scoped>
.login-form {
  margin: 100px auto;
  width: 450px;
}
.title{
  margin: 20px;
  text-align: center;
}
</style>